package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.Formation;
import modele.Session;
import modele.Theme;
import dao.FormationDAO;
import dao.FormationThemeDAO;
import dao.SessionDAO;

/**
 * Servlet implementation class RechercherFormation
 */
public class RechercherFormation extends HttpServlet {

	private static final long	serialVersionUID	= 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RechercherFormation() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * Recupere le mot de recherche
	 * Cree une collection de formations par rapport aux recherche
	 * Visualise les sessions ouvertes et futures
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String ajax = request.getParameter("ajax");
		String recherche = request.getParameter("recherche");
		String rechercheParThemeCheckbox = request.getParameter("rechercheParTheme");

		boolean rechercheParTheme = false;
		if (rechercheParThemeCheckbox != null && rechercheParThemeCheckbox.equals("on")) {
			rechercheParTheme = true;
			request.setAttribute("rechercheParThemeChecked", "checked=\"checked\"");
		}

		ArrayList<Container> containerList = getContainerList(recherche, rechercheParTheme);
		if (containerList != null) {
			request.setAttribute("containerList", containerList);
		}

		if (ajax != null && ajax.equals("on")) {
			System.out.println("ajax " + recherche);
			getServletContext().getRequestDispatcher("/WEB-INF/rechercheResultsAjax.jsp").forward(request, response);
		} else {
			getServletContext().getRequestDispatcher("/WEB-INF/rechercheResults.jsp").forward(request, response);
		}
	}

	public ArrayList<Container> getContainerList(String recherche, boolean rechercheParTheme) {

		if (recherche != null && !recherche.isEmpty() && !recherche.matches("^\\s*$")) {

			ArrayList<Formation> formations = new ArrayList<Formation>();

			ArrayList<Container> containerList = new ArrayList<>();

			try {

				formations = (ArrayList<Formation>) FormationDAO.rechercheMultiWithTheme(recherche, rechercheParTheme);

				SessionDAO sessionDao = new SessionDAO();

				for (Formation formation : formations) {

					System.out.println(formation.getListTitle());

					int idFormation = formation.getIdFormation();

					ArrayList<Session> sessionsOuvertes = (ArrayList<Session>) sessionDao.listerSessionsOuvertesRecherche(idFormation);
					ArrayList<Session> sessionsFutures = (ArrayList<Session>) sessionDao.listerSessionsFuturesRecherche(idFormation);

					/*
					System.out.println("----sessions for formation id: " + idFormation);
					for (Session session : sessionsOuvertes) {
						System.out.println(session);
					}
					*/

					ArrayList<Theme> themes = FormationThemeDAO.getFormationThemes(formation);

					containerList.add(new Container(formation, sessionsOuvertes, sessionsFutures, themes));

				}

				return containerList;

			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("recherche is empty");
		}

		return null;

	}

	public class Container {

		public Formation			formation;
		public ArrayList<Session>	sessionsOuvertes;
		public ArrayList<Session>	sessionsFutures;
		public ArrayList<Theme>		themes;

		/**
		 * Construit un objet Container pour transmettre formation, sessions
		 * ouvertes et futures au rechercheResultsAjax.jsp
		 * 
		 * @param formation
		 * @param sessionsOuvertes
		 * @param sessionsFutures
		 * @param themes
		 */
		public Container(Formation formation, ArrayList<Session> sessionsOuvertes, ArrayList<Session> sessionsFutures, ArrayList<Theme> themes) {
			super();
			this.formation = formation;
			this.sessionsOuvertes = sessionsOuvertes;
			this.sessionsFutures = sessionsFutures;
			this.themes = themes;
		}

		public Formation getFormation() {
			return formation;
		}

		public void setFormation(Formation formation) {
			this.formation = formation;
		}

		public ArrayList<Session> getSessionsOuvertes() {
			return sessionsOuvertes;
		}

		public void setSessionsOuvertes(ArrayList<Session> sessionsOuvertes) {
			this.sessionsOuvertes = sessionsOuvertes;
		}

		public ArrayList<Session> getSessionsFutures() {
			return sessionsFutures;
		}

		public void setSessionsFutures(ArrayList<Session> sessionsFutures) {
			this.sessionsFutures = sessionsFutures;
		}

		public ArrayList<Theme> getThemes() {
			return themes;
		}

		public void setThemes(ArrayList<Theme> themes) {
			this.themes = themes;
		}

	}

}
